package com.elangzhi.ssm.modules.goods.app;

import com.elangzhi.ssm.controller.util.ParamMap;
import com.elangzhi.ssm.model.Goods;
import com.elangzhi.ssm.model.Image;
import com.elangzhi.ssm.model.Shop;
import com.elangzhi.ssm.modules.collection.services.CollectionService;
import com.elangzhi.ssm.modules.goods.services.GoodsService;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.shop.services.ShopService;
import com.elangzhi.ssm.modules.shopEvent.services.ShopEventService;
import com.elangzhi.ssm.modules.user.services.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 商品 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/goods")
public class AppGoodsController {

    /**
     * 获取首页banner
     * @return
     */
    @RequestMapping("/indexBanner")
    @ResponseBody
    public List<Image> indexBanner(){
        return imageService.listByTypeInfo(0,null);
    }

    /**
     * 根据地区查找
     * @param request 请求参数
     * @param page 当前页数
     * @param size 每页大小
     * @return 数据列表
     */
    @RequestMapping("/listByRegion")
    @ResponseBody
    public ParamMap listByRegion(HttpServletRequest request,
                                 @RequestParam("page") Integer page,
                                 @RequestParam("size") Integer size){
        ParamMap map = new ParamMap(request);
        List<Goods> list = goodsService.listByRegion(map,page,size);
        for(Goods goods : list){
            // 添加bannerList图片列表
            goods.setBannerList(imageService.listByTypeInfo(2,goods.getId()));
            // 添加shop对象
            goods.setShop(shopService.selectById(goods.getShopId()));

        }
        map.put("list",list);
        return map;
    }

    /**
     * 根据id查找
     * @param id 数据id
     * @param userId 用户id
     * @return 数据实体数据，带图片列表
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Goods findById(@RequestParam("id") Long id,@RequestParam(value = "userId",required = false) Long userId){
        Goods goods = goodsService.selectById(id);
        if(goods != null){
            // 添加bannerList图片列表
            goods.setBannerList(imageService.listByTypeInfo(2,id));
            // 给商品添加当前活动
            goods.setEventList(shopEventService.listByGoodsId(id));

            // 添加店铺信息和用户信息
            Shop shop = shopService.selectById(goods.getShopId());
            goods.setShop(shop);
            if(shop != null){
                goods.setUser(userService.selectById(shop.getUserId()));
            }
            // 添加收藏信息
            if(userId != null){
                goods.setCollection(collectionService.findByGoodsIdAndUserId(id,userId));
            }
        }
        return goods;
    }

    //---------------------------- property -------------------------------

    @Resource
    private GoodsService goodsService;

    @Resource
    private ShopService shopService;

    @Resource
    private UserService userService;

    @Resource
    private ImageService imageService;

    @Resource
    private ShopEventService shopEventService;

    @Resource
    private CollectionService collectionService;

}
